<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ProductController
 *
 * @author NGOC SANG
 */
class ProductController extends HomeController {
    //put your code here
    public $_model;

    public function loadModel(){
                if($this->_model===null)
                {
                        if(isset($_GET['id']))
                                $this->_model=ProductCategory::model()->findbyPk($_GET['id']);
                        if($this->_model===null)
                                throw new CHttpException(404,'The requested page does not exist.');
                }
                return $this->_model;
        }
    public function actionDetail() {

    $baseUrl = Yii::app()->baseUrl;
    $cs = Yii::app()->getClientScript();
    $cs->registerScriptFile($baseUrl.'/themes/default/js/jquery-ui.js');
    $model = Product::model()->findbyPk($_GET['id']);
    $this->render('_detail', array(
      'data' => $model,
    ));

    }
    public function actionCategory(){
		$catId = Yii::app()->request->getParam('id','');
		$this->render('category', array(
				'catID'=>$catId
		));
	}        
    private function _selectListWithIndent() {
        $categories = ProductCategory::model()->findAll(array('order' => 'root,lft'));
        $level = 0;
        $indent = '';
        $listData = array();
        $listData['']='';
        foreach ($categories as $n => $category) {
            if ($category->level == $level) {
                
            } elseif ($category->level > $level) {
                $indent.='--';
            } else {
                $indent = substr($indent, -2);
            }
            $listData[$category->id] = $indent . $category->name;
            $level = $category->level;
        }
        return $listData;
    }
    public function actionSearch(){        
        $keyword = isset($_GET['keyword'])?$_GET['keyword']:'';	
         if (!empty($keyword)) {
//            $criteria = new CDbCriteria();
//            $criteria->condition = 'name like "%' . $keyword . '%"';
//            $criteria->order = 'id DESC';
//            $dataProvider = new CActiveDataProvider('Product', array('pagination' => array(
//                            'pageSize' => 50,
//                        ),
//                        'criteria' => $criteria,
//                    ));
//            
//            $this->render('index', array(
//            'model' => $dataProvider,  
//            'listData' => $this->_selectListWithIndent()
//             
//        ));
                $model=new Product('search');
                $model->unsetAttributes(); // clear any default values
                if (isset($_GET['Product']))
                    $model->attributes = $_GET['Product'];

                $this->render('index', array(
                        'model'=>$model,
                        'listData' => $this->_selectListWithIndent()
                ));
         
        } else {
            throw new CHttpException(400, 'Ban phai nhap tu khoa tim kiem');
        }                
             
    }
}

?>
